package com.engine4cloud.tdf.supervision.controller.web;

import cn.hutool.core.collection.CollUtil;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.supervision.dto.IdentityUniqueCheckDTO;
import com.engine4cloud.tdf.supervision.dto.MobileUniqueCheckDTO;
import com.engine4cloud.tdf.supervision.dto.create.GzrySocialWorkerCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.GzrySocialWorkerQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.GzrySocialWorkerResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.GzrySocialWorkerUpdateDTO;
import com.engine4cloud.tdf.supervision.service.GzrySocialWorkerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 社会工作者
 *
 * @author wenjie
 * @date 2024-04-15 11:35:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/web/social/worker")
@Tag(description = "SocialWorker", name = "工作人员-社会工作者-web")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class GzrySocialWorkerWebController {

    private final GzrySocialWorkerService socialWorkerService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param dto  社会工作者
     * @return R<PageInfo < GzrySocialWorkerResultDTO>>
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R<PageInfo<GzrySocialWorkerResultDTO>> getPageList(@ParameterObject Params page, @Valid @ParameterObject GzrySocialWorkerQueryDTO dto) {
        return R.ok(socialWorkerService.getPageList(page, dto));
    }


    /**
     * 通过id查询社会工作者
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R<GzrySocialWorkerResultDTO> getById(@PathVariable("id") Long id) {
        return R.ok(socialWorkerService.getById(id));
    }

    /**
     * 新增社会工作者
     *
     * @param dto 社会工作者
     * @return R
     */
    @Operation(summary = "新增社会工作者", description = "新增社会工作者")
    @SysLog("新增社会工作者")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('worker_add')")
    public R<Boolean> create(@Valid @RequestBody GzrySocialWorkerCreateDTO dto) {
        return R.ok(socialWorkerService.create(dto));
    }

    /**
     * 修改社会工作者
     *
     * @param dto 社会工作者
     * @return R
     */
    @Operation(summary = "修改社会工作者", description = "修改社会工作者")
    @SysLog("修改社会工作者")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('worker_edit')")
    public R<Boolean> update(@Valid @RequestBody GzrySocialWorkerUpdateDTO dto) {
        return R.ok(socialWorkerService.update(dto));
    }

    /**
     * 通过id删除社会工作者
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除社会工作者", description = "通过id删除社会工作者")
    @SysLog("通过id删除社会工作者")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('worker_del')")
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(socialWorkerService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 证件号是否唯一
     *
     * @param dto dto
     * @return 返回 true-唯一；false-不唯一
     */
    @Operation(summary = "证件号是否唯一", description = "证件号是否唯一")
    @PostMapping("/check/identity")
    public R<Boolean> checkIdentityUnique(@Valid @RequestBody IdentityUniqueCheckDTO dto) {
        return R.ok(socialWorkerService.checkUniqueByIdentity(dto));
    }

    /**
     * 手机号是否唯一
     *
     * @param dto dto
     * @return 返回 true-唯一；false-不唯一
     */
    @Operation(summary = "手机号是否唯一", description = "手机号是否唯一")
    @PostMapping("/check/mobile")
    public R<Boolean> checkMobileUnique(@Valid @RequestBody MobileUniqueCheckDTO dto) {
        return R.ok(socialWorkerService.checkUniqueByMobile(dto));
    }
}